package ja8.steam;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SteamTest {
    private static List<Goods> goodsList = new ArrayList<>();;
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            Goods goods = new Goods();
            goods.setId(i+"");
            goods.setName(i+"");
            goods.setType(i/4+"");
            goods.setPrice(i*10.00);
            goodsList.add(goods);
            System.out.println(goods);
        }

        Stream<Goods> stream = goodsList.stream();
        //分组
        Map<String, Set<Goods>> collect = goodsList.stream().collect(Collectors.groupingBy(goods -> goods.getType(),Collectors.toSet()));
        List<Goods> collect1 = stream.sorted(((o1, o2) -> o1.getPrice() > o2.getPrice() ? -1 : 1)).collect(Collectors.toList());
        Map<String, Goods> collect2 = goodsList.stream().collect(
                Collectors.groupingBy(
                        goods -> goods.getType(),
                Collectors.collectingAndThen(
                        Collectors.reducing((goods, goods2) -> goods.getPrice() > goods2.getPrice() ? goods : goods2), Optional::get)
                )
        );
        System.out.println();
    }
}
